#include "smart-device-protocol.h"
#include <stdio.h>
#include <iostream>
#include <stdint.h>

void printVectorAsHex(const std::vector<uint8_t> &vec)
{
	for(size_t i = 0; i < vec.size(); ++i)
	{
		printf("%02X ", vec[i]); // %02X 格式化为两位16进制数，不足两位前面补0
	}
	printf("\n");
}

int main(int argc, char const *argv[])
{
	SmartDeviceProtocol doorlockProtocol = SmartDeviceProtocol::
	    makeDoorLockProtocol(0x01, 0x00, 0x00);
	doorlockProtocol.showPacket();

	cout << "-------------------------" << endl;
	vector<uint8_t> packet(doorlockProtocol.packetData(), doorlockProtocol.packetData() + doorlockProtocol.packetLenth());

	// printVectorAsHex(packet);
	SmartDeviceProtocol ackProtocol = SmartDeviceProtocol::makeProtocolFromPacket(packet);

	// 4个16进制显示 [0] 成员高字节 [1] 低字节
	printf("packet head : %04x\n", ackProtocol.getPacketHead());
	printf("packet addr : %08x\n", ackProtocol.getPacketAddr());
	printf("packet id   : %02x\n", ackProtocol.getPacketID());
	printf("packet len  : %04x\n", ackProtocol.getPacketDataLen());
	printf("packet data : ");
	uint16_t datalen = ackProtocol.getPacketDataLen();
	uint8_t *data    = ackProtocol.getPacketData();
	for(int i = 0; i < datalen; i++)
	{
		printf("%02x ", data[i]);
	}
	printf("\n");
	printf("packet sum: %04x\n", ackProtocol.getPacketCRC16());

	return 0;
}
